﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace CarBlackBox
{
    public partial class Form_SetDriverIDCard : Form
    {
        bool bClose = true;
        private DriverInfoData Driver = new DriverInfoData();
        public Form_SetDriverIDCard(DriverInfoData d)
        {
            InitializeComponent();
            Driver = d;
        }

        private void Form_SetDriverIDCard_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Driver.DIN))
            {
                this.Close();
                return;
            }
            tbName.Text = Driver.Name;
            tbDriverID.Text = Driver.DriverID.ToString();
            tbDIN.Text = Driver.DIN;
            cbU.Items.Clear();
            foreach (DriveInfo d in DriveInfo.GetDrives())
            {
                if (d.DriveType == DriveType.Removable)
                    cbU.Items.Add(d.Name);
            }
            if (cbU.Items.Count > 0)
                cbU.SelectedIndex = 0;
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.Add("默认端口");
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            cbU.Items.Clear();
            foreach (DriveInfo d in DriveInfo.GetDrives())
            {
                if (d.DriveType == DriveType.Removable || !checkBox1.Checked)
                    cbU.Items.Add(d.Name);
            }
            if (cbU.Items.Count > 0)
                cbU.SelectedIndex = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (WFNetLib.WFGlobal.MessageYseOrNo("确定取消当前操作么？", "设置身份卡操作取消") == DialogResult.No)
                bClose = false;
        }

        private void Form_SetDriverIDCard_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!bClose)
            {
                e.Cancel = true;
                bClose = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)//串口
            {
                this.Cursor = Cursors.WaitCursor;
                SerialCommunication com;
                if (cbCom.SelectedIndex == 0)
                    com = new SerialCommunication(this);
                else
                    com = new SerialCommunication(cbCom.Text, this);
                com.ProcCommand(CommunicationCommand.设置驾驶员代码_驾驶证号码, tbDriverID.Text, tbDIN.Text);
                this.Cursor = Cursors.Default;
                WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                log.LogType = LogType.设置记录仪.ToString();
                log.LogContent = "串口设置:设置驾驶员代码_驾驶证号码:" + tbDriverID.Text + "," + tbDIN.Text + "," + tbName.Text;
                log.LogRemark = "记录仪";// +tbRIN.Text;
                log.InserSQLLog();
            }
            this.DialogResult = DialogResult.OK;
        }
    }
}
